﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AutomaticInterferenceVolumeCreation.CApAIVCInterferenceZone
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.AutomaticInterferenceVolumeCreation.Resources;
using DnProcessSimulateCommands.SweptVolume;
using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.AutomaticInterferenceVolumeCreation;

internal class CApAIVCInterferenceZone : CApSVInterferenceZone
{
  private bool m_shouldAbort;
  private List<string> m_errors;

  internal event LowLevelStepProgressEventHandler LowLevelStepProgress;

  public CApSVInterferenceZoneData[] InterferenceZones
  {
    get
    {
      CApSVInterferenceZoneData[] interferenceZones = (CApSVInterferenceZoneData[]) null;
      if (this.m_samplingManager != null)
        interferenceZones = this.m_samplingManager.InterferenceZones;
      return interferenceZones;
    }
  }

  public List<string> Errors => this.m_errors;

  public CApAIVCInterferenceZone()
  {
    this.m_shouldAbort = false;
    this.m_errors = new List<string>();
  }

  public void abort()
  {
    this.UnregisterEvents();
    this.m_shouldAbort = true;
    if (this.m_samplingManager == null)
      return;
    this.m_samplingManager.stopSampling();
  }

  public TxInterferenceVolumeLocationDataEx CreateInterferenceVolumeLocationData(
    TxInterferenceVolume interferenceVolume,
    CApSVInterferenceZoneGateData zoneGate)
  {
    TxInterferenceVolumeLocationDataEx.TxLocationType type = (TxInterferenceVolumeLocationDataEx.TxLocationType) 2;
    ITxRoboticLocationOperation locationOperation = (ITxRoboticLocationOperation) null;
    switch (zoneGate.GateType)
    {
      case EApSVInterferenceZoneGateType.ENTER:
        if (zoneGate.InsideInterference)
        {
          type = (TxInterferenceVolumeLocationDataEx.TxLocationType) 2;
          locationOperation = zoneGate.LocationInside as ITxRoboticLocationOperation;
          break;
        }
        type = (TxInterferenceVolumeLocationDataEx.TxLocationType) 0;
        locationOperation = zoneGate.LocationOutside as ITxRoboticLocationOperation;
        break;
      case EApSVInterferenceZoneGateType.EXIT:
        if (zoneGate.InsideInterference)
        {
          type = (TxInterferenceVolumeLocationDataEx.TxLocationType) 3;
          locationOperation = zoneGate.LocationInside as ITxRoboticLocationOperation;
          break;
        }
        type = (TxInterferenceVolumeLocationDataEx.TxLocationType) 1;
        locationOperation = zoneGate.LocationOutside as ITxRoboticLocationOperation;
        break;
    }
    TxRoboticCommandCreationData commandCreationData = new TxRoboticCommandCreationData(this.GetRoboticCommandText(interferenceVolume, type));
    TxRoboticCommand command = ((ITxRoboticOperation) locationOperation).CreateCommand(commandCreationData);
    return new TxInterferenceVolumeLocationDataEx((ITxLocationOperation) locationOperation, type, command);
  }

  protected override void initSamplingManager()
  {
    base.initSamplingManager();
    this.m_samplingManager.SimulationHandler.AutomaticErrorOutput = false;
    this.m_samplingManager.SupportInsideZones = true;
    this.m_samplingManager.LowLevelStepProgress += new LowLevelStepProgressEventHandler(this.OnLowLevelProgress);
  }

  protected override void onSamplingEnded()
  {
    this.m_errors = this.m_samplingManager.SimulationHandler.Errors;
    this.UnregisterEvents();
  }

  private void OnLowLevelProgress(object sender, EventArgs e)
  {
    if (this.LowLevelStepProgress == null)
      return;
    this.LowLevelStepProgress(sender, e);
  }

  private void UnregisterEvents()
  {
    this.m_samplingManager.LowLevelStepProgress -= new LowLevelStepProgressEventHandler(this.OnLowLevelProgress);
  }

  private string GetRoboticCommandText(
    TxInterferenceVolume interferenceVolume,
    TxInterferenceVolumeLocationDataEx.TxLocationType type)
  {
    string str = "";
    switch ((int) type)
    {
      case 0:
        str = AIVCResources.AIVC_LAST_LOC_BEFORE;
        break;
      case 1:
        str = AIVCResources.AIVC_FIRST_LOC_AFTER;
        break;
      case 2:
        str = AIVCResources.AIVC_FIRST_LOC_IN;
        break;
      case 3:
        str = AIVCResources.AIVC_LAST_LOC_IN;
        break;
    }
    return $"{str} {interferenceVolume.Name}";
  }
}
